package com.ruoyi.H5Send.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

/**
 *
 * @author tianyh
 * @Description:HTTP 请求
 */

public class ChuangLanSmsUtil {
    /**
     *
     * @author tianyh
     * @Description
     * @param path
     * @param postContent
     * @return String
     * @throws
     */
    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");

//			PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
//			printWriter.write(postContent);
//			printWriter.flush();

            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();

            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                // 开始获取数据
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int LENGTH = 4;

    /**
     * 生成一个随机的四位验证码
     * @return 验证码字符串
     */
    public static String getRandomYZM() {
        Random random = new Random();

        // 创建一个StringBuilder来构建最终的验证码字符串
        StringBuilder sb = new StringBuilder();

        // 循环四次，每次生成一个0-9之间的随机数并添加到StringBuilder中
        for (int i = 0; i < 4; i++) {
            int num = random.nextInt(10); // 生成0到9之间的随机数
            sb.append(num); // 将随机数添加到StringBuilder中
        }

        // 将StringBuilder转换为String并返回
        return sb.toString();
    }
//
//    public static void main(String[] args) {
//        // 测试生成验证码
//        for (int i = 0; i < 10; i++) {
//            System.out.println(getRandomYZM());
//        }
//    }

}
